//
//  PlayView.m
//  27-Block练习播放按钮
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 m. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
@interface PlayView ()
@property (nonatomic, strong)UIButton *playbutton;
@property (nonatomic, strong)UIButton *nextbutton;
@property (nonatomic, strong)UIButton *lastbutotn;
@property (nonatomic, copy)void (^play)(UIButton *play);
@property (nonatomic, copy)void (^pause)(UIButton *pause);
@property (nonatomic, copy)void (^last)(UIButton *last);
@property (nonatomic, copy)void (^next)(UIButton *next);
@end
@implementation PlayView
- (instancetype)initWithFrame:(CGRect)frame {
      self = [super initWithFrame:frame];
      if (self) {
            [self setUp];
      }
      return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
      self = [super initWithCoder:aDecoder];
      if (self) {
            [self setUp];
      }
      return self;
}
- (void)setUp {
      _playbutton = [self ButtonImageWithName:@"Unknown-2" action:@selector(play:)];
      _lastbutotn = [self ButtonImageWithName:@"Unknown-1" action:@selector(last:)];
      _nextbutton = [self ButtonImageWithName:@"Unknown-3" action:@selector(next:)];
      
}
- (void)layoutSubviews {
      [super layoutSubviews];
      _playbutton.frame = CGRectMake(0, 0, 100, 100);
      _playbutton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
      _lastbutotn.centerY = _playbutton.centerY;
      _lastbutotn.width = 50;
      _lastbutotn.height = 50;
      _lastbutotn.x = _playbutton.centerX - (_lastbutotn.width / 2 + _playbutton.width / 2 + 50);
      _nextbutton.centerY = _playbutton.centerY;
      _nextbutton.width = 50;
      _nextbutton.height = 50;
      _nextbutton.x = _playbutton.centerX + (_lastbutotn.width / 2 + _playbutton.width / 2);

      
}
//播放
- (void)play:(UIButton *)playButton {
      [playButton setImage:[UIImage imageNamed:@"Unknown-8"] forState:UIControlStateNormal];
      [playButton removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
      [playButton addTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
      self.play(playButton);
}
//暂停
- (void)pasue:(UIButton *)pasueButton {
      [pasueButton setImage:[UIImage imageNamed:@"Unknown-2"] forState:UIControlStateNormal];
      [pasueButton removeTarget:self action:@selector(pasue:) forControlEvents:UIControlEventTouchUpInside];
      [pasueButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
      self.pause(pasueButton);

}
//上一首
- (void)last:(UIButton *)last {
      self.last(last);

}
//下一首
- (void)next :(UIButton *)next {
      self.next(next);
}
- (void)play:(void (^)(UIButton *play))play pause:(void (^)(UIButton *pause))pause last:(void (^)(UIButton *))last next:(void (^)(UIButton *))next{
      self.play = play;
      self.pause = pause;
      self.last = last;
      self.next = next;
}
- (UIButton *)ButtonImageWithName :(NSString *)imageName action:(SEL)action{
      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
      [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
      [self addSubview:button];
      return button;
}
@end
